package aex.util;

import java.util.Set;
import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.StrutsStatics;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
/**
 * 管理员包拦截器
 * @author JiangYouYao
 */
public class AdminInterceptor implements Interceptor {

	private static final long serialVersionUID = -5178310397732210602L;

	public void destroy() {
	}

	public void init() {
	}

	public String intercept(ActionInvocation invocation) throws Exception {
		
		ActionContext actionContext = invocation.getInvocationContext();
		HttpServletRequest request = (HttpServletRequest) actionContext
				.get(StrutsStatics.HTTP_REQUEST);
		CU.deBugPrintln("进入拦截器"+request.getRequestURI());
		lookMemory();
		String path=request.getRequestURI();
		boolean flag=false;
		//定义要放行的action
		Set<String> able=new TreeSet<String>();
		able.add(request.getContextPath() +"/manage/admin_login.do");
		if(request.getSession().getAttribute("admin") != null){
			flag=true;
		}else if(able.contains(path)){
			flag=true;
		}
		if (flag) {
			CU.deBugPrintln("拦截器放行");
			return invocation.invoke();
		} else {
			CU.deBugPrintln("拦截器禁止同通行！");
			request.setAttribute("err", "登陆超时，请重新登陆！");
			return "exit";
		}
		
		
		
	}

	private void lookMemory() {
		Runtime run = Runtime.getRuntime();

		long max = run.maxMemory();

		long total = run.totalMemory();

		long free = run.freeMemory();

		long usable = max - total + free;

		CU.deBugPrintln("最大内存 = " + max/1024/1024);
		CU.deBugPrintln("已分配内存 = " + total/1024/1024);
		CU.deBugPrintln("已分配内存中的剩余空间 = " + free/1024/1024);
		CU.deBugPrintln("最大可用内存 = " + usable/1024/1024);
	}

}
